<?php

namespace StudyBuddy;

/**
 * Class for generating html of the registration block
 * with registration form
 * This html block is usually shown inside the modale window
 *
 */
class RegBlock extends StudyBuddyObject {

    protected $aUsername = array(
        'usernameLabel' => 'Username',
        'usernameVal' => '',
        'usernameNote' => 'Username will appear alongside your posts');
    protected $tplOptin = '<tr>
	<td>
	<div class="tr">
	<input type="checkbox" id="optin" name="optin" %3$s/>&nbsp;%1$s
	<div class="note2">%2$s</div>
	</div>
	</td>
	</tr>';
    protected $aOptin = array(
        'listName' => 'API News ',
        'listNote' => 'Receive our email newsletter about interesting developments in Social Media, Open source projects and APIs',
        'isChecked' => ''
    );
    protected $oRegistry;

    /**
     * Object of tyle User
     * or any subclass
     * representing currenly logged in user
     *
     * @var object
     */
    protected $oViewer;

    /**
     * Array of replacement vars that will
     * be set in template tplRegform.php
     *
     * @var array
     */
    protected $aVars = array();

    public function __construct(Registry $oRegistry) {

        $this->oRegistry = $oRegistry;
        $this->oViewer = $oRegistry->Viewer;
    }

    /**
     * Makes an object which is sub-class of this
     * class, depending on the type of oViewer
     * we have: if user just logged in with Twitter
     * then we create object RegBlockTwitter
     * which has some specific html for Twitter user,
     * if it's a type of Google FriendConnect, then of type
     * RegBlockGfc, etc.
     *
     * @param Registry oRegistry
     *
     * @return object of this class or subclass
     */
    public static function factory(Registry $oRegistry) {
        $oViewer = $oRegistry->Viewer;
        switch (true) {
            case ($oViewer instanceof UserTwitter):
                $o = new RegBlockTwitter($oRegistry);
                break;

            case ($oViewer instanceof UserGfc):
                $o = new RegBlockGfc($oRegistry);
                break;

            case ($oViewer instanceof UserLinkedin):
                $o = new RegBlockLinkedin($oRegistry);
                break;

            default:
                $o = new RegBlockTwitter($oRegistry);
        }

        return $o;
    }

    /**
     * Create html block for registration form
     *
     * @return string HTML of registration block
     */
    public function getBlock() {
        $this->prepareVars()
                ->setUsernameVars()
                ->addUsernameBlock();
        d('cp $this->aVars ' . print_r($this->aVars, 1));
        $ret = \tplRegform::parse($this->aVars);
        d('$ret: ' . $ret);

        return $ret;
    }

    /**
     * Makes the block for the 'Username' Form
     * field and sets the new block
     *
     * @return object $this;
     */
    protected function addUsernameBlock() {
        /* d('cp $this->aUsername: '.print_r($this->aUsername, 1));
          $this->aVars['usernameBlock'] = \tplUsernameblock::parse($this->aUsername, false);//vsprintf($this->tplUsername, $this->aUsername);
          d('$this->aVars[usernameBlock]: '.print_r($this->aVars['usernameBlock'], 1) );
         */

        return $this;
    }

    /**
     * @todo change to NOT use usernameBLock
     * and instead just directly set vars in tplRegform
     * values of 'username'
     * 
     */
    protected function setUsernameVars() {
        d('cp');
        $this->aUsername = array('Username', $this->oViewer->username, 'Username will appear alongside your posts');
        d('$this->aUsername: ' . print_r($this->aUsername, 1));

        $this->aVars['username'] = $this->oViewer['username'];

        return $this;
    }

    /**
     * Set values of replacement vars for the template
     * depending on the type of oViewer object
     * If it's isNewUser and already has external avatar
     * then also add block and show the name of external
     * auth provider like "Twitter"
     *
     * If this is a brand new registration then we also
     * need to add Captcha image and hidden field
     * and extra input text field
     */
    protected function prepareVars() {

        return $this;
    }

}
